/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.haipv.gtkhoa501.controller;

import com.haipv.gtkhoa501.model.Dethi;
import com.haipv.gtkhoa501.model.Exam;
import com.haipv.gtkhoa501.model.Question;
import com.haipv.gtkhoa501.model.Signs;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.Vector;

/**
 *
 * @author PHANHAI
 */
public class StorageData {

    private final int NUM_BIEN = 105;
    public final int NUM_CAUHOI = 450;
    public final int NUM_EXAM = 30;
    public final int NUM_TUBE = 20;
    private static StorageData instance = null;
    private Vector exams = null;

    private StorageData() {
    }

    /**
     * Lấy câu hỏi thứ index.
     *
     * @param index
     * @return
     */
    public Exam getExam(int index) {
        int i = index -1 ;
        return (Exam) exams.elementAt(i);
    }

    public int getResult() {
        if (exams == null) {
            return 0;
        }
        int result = 0;
        for (int i = 0; i < NUM_EXAM; ++i) {
            Exam exam = (Exam) exams.elementAt(i);
            if (exam.checkAnswer()) {
                result++;
            }
        }
        return result;
    }

    public static StorageData getInstance() {
        if (instance == null) {
            instance = new StorageData();
        }
        return instance;
    }

    /**
     * Lấy biển báo có mã_id 3
     *
     * @param _id
     * @return
     */
    public Signs getSign(int _id) {

        InputStream in = this.getClass().getResourceAsStream("/data/bienbao_v1.dat");
        DataInputStream dis = new DataInputStream(in);
        //
        int id;
        int type;
        String img;
        String title;
        String content;
        //
        try {
            for (int i = 0; i < NUM_BIEN; ++i) {
                id = dis.readInt();
                type = dis.readInt();
                img = dis.readUTF();
                title = dis.readUTF();
                content = dis.readUTF();
                if (id == _id) {
                    return new Signs(id, type, img, title, content);
                }
                //System.out.println("========\n " + id + " " + type + " " + img + " " + title + " " + content);

            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return null;
    }

    /**
     * Lấy danh sách biển báo loại type từ kho dữ liệu. 2
     *
     * @return
     */
    public Vector getSigns(int _type) {
        Vector signs = new Vector(NUM_BIEN);

        InputStream in = this.getClass().getResourceAsStream("/data/bienbao_v1.dat");
        DataInputStream dis = new DataInputStream(in);
        //
        int id;
        int type;
        String img;
        String title;
        String content;
        //
        try {
            for (int i = 0; i < NUM_BIEN; ++i) {
                id = dis.readInt();
                type = dis.readInt();
                img = dis.readUTF();
                title = dis.readUTF();
                content = dis.readUTF();
                if (type != _type) {
                    continue;
                }
                //System.out.println("========\n " + id + " " + type + " " + img + " " + title + " " + content);
                Signs sign = new Signs(id, type, img, title, content);
                signs.addElement(sign);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return signs;
    }

    /**
     * Lấy danh sách biển báo từ kho dữ liệu. 1
     *
     * @return
     */
    public Vector getSigns() {
        Vector signs = new Vector(NUM_BIEN);

        InputStream in = this.getClass().getResourceAsStream("/data/bienbao_v1.dat");
        DataInputStream dis = new DataInputStream(in);
        //
        int id;
        int type;
        String img;
        String title;
        String content;
        //
        try {
            for (int i = 0; i < NUM_BIEN; ++i) {
                id = dis.readInt();
                type = dis.readInt();
                img = dis.readUTF();
                title = dis.readUTF();
                content = dis.readUTF();
                //System.out.println("========\n " + id + " " + type + " " + img + " " + title + " " + content);
                Signs sign = new Signs(id, type, img, title, content);
                signs.addElement(sign);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return signs;
    }

    /**
     * Lấy danh sách câu hỏi từ kho dữ liệu
     *
     * @return
     */
    public Question getQuestion(int _id) {

        InputStream in = this.getClass().getResourceAsStream("/data/cauhoi_v1.dat");
        DataInputStream dis = new DataInputStream(in);
        //
        int id;
        String content;
        String opt1;
        String opt2;
        String opt3;
        String opt4;
        String img;
        int answer;
        int type;
        //
        try {
            for (int i = 0; i < NUM_CAUHOI; ++i) {
                id = dis.readInt();
                content = dis.readUTF();
                opt1 = dis.readUTF().trim();
                opt2 = dis.readUTF().trim();
                opt3 = dis.readUTF().trim();
                opt4 = dis.readUTF().trim();
                img = dis.readUTF().trim();
                answer = dis.readInt();
                type = dis.readInt();
                Vector options = new Vector();

                if (opt1.length() != 0) {
                    options.addElement(opt1);
                }
                if (opt2.length() != 0) {
                    options.addElement(opt2);
                }
                if (opt3.length() != 0) {
                    options.addElement(opt3);
                }
                if (opt4.length() != 0) {
                    options.addElement(opt4);
                }
                //System.out.println("========\n " + id + content + " " + opt1 + " " + img + " " + opt2 + " " + opt3);
                if (id == _id) {
                    return new Question(id, content, options, img, type, answer);
                }
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return null;
    }

    /**
     * Lấy danh sách câu hỏi từ kho dữ liệu
     *
     * @return
     */
    public Vector getQuestion() {
        Vector questions = new Vector(NUM_CAUHOI);

        InputStream in = this.getClass().getResourceAsStream("/data/cauhoi_v1.dat");
        DataInputStream dis = new DataInputStream(in);
        //
        int id;
        String content;
        String opt1;
        String opt2;
        String opt3;
        String opt4;
        String img;
        int answer;
        int type;
        //
        try {
            for (int i = 0; i < NUM_CAUHOI; ++i) {
                id = dis.readInt();
                content = dis.readUTF();
                opt1 = dis.readUTF().trim();
                opt2 = dis.readUTF().trim();
                opt3 = dis.readUTF().trim();
                opt4 = dis.readUTF().trim();
                img = dis.readUTF().trim();
                answer = dis.readInt();
                type = dis.readInt();
                Vector options = new Vector();

                if (opt1.length() != 0) {
                    options.addElement(opt1);
                }
                if (opt2.length() != 0) {
                    options.addElement(opt2);
                }
                if (opt3.length() != 0) {
                    options.addElement(opt3);
                }
                if (opt4.length() != 0) {
                    options.addElement(opt4);
                }
                //System.out.println("========\n " + id + content + " " + opt1 + " " + img + " " + opt2 + " " + opt3);
                Question question = new Question(id, content, options, img, type, answer);
                questions.addElement(question);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return questions;
    }

    /**
     * Lấy bộ câu hỏi lmaf bài thi.
     */
    public void loadExams(int bd) {
        exams = new Vector(NUM_EXAM);

        InputStream in1 = this.getClass().getResourceAsStream("/data/bode" + bd + ".dat");
        DataInputStream dis1 = new DataInputStream(in1);

        Dethi dt = new Dethi();
        int id_ch = 1 + (new Random()).nextInt(NUM_TUBE);
        try {
            int _id;
            for (int i = 0; i < NUM_TUBE; ++i) {
                _id = dis1.readInt();
                if (_id != id_ch) {
                    for (int j = 0; j < NUM_EXAM; ++j) {
                        dis1.readInt();
                    }
                } else {
                    dt.id = _id;
                    for (int j = 0; j < NUM_EXAM; ++j) {
                        dt.cauhoi[j] = dis1.readInt();
                    }
                    break;
                    
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }


        InputStream in = this.getClass().getResourceAsStream("/data/cauhoi_v1.dat");
        DataInputStream dis = new DataInputStream(in);
        //
        int id;
        String content;
        String opt1;
        String opt2;
        String opt3;
        String opt4;
        String img;
        int answer;
        int type;
        int index = 0;
        //
        try {
            for (int i = 0; i < NUM_CAUHOI; ++i) {
                id = dis.readInt();
                content = dis.readUTF();
                opt1 = dis.readUTF().trim();
                opt2 = dis.readUTF().trim();
                opt3 = dis.readUTF().trim();
                opt4 = dis.readUTF().trim();
                img = dis.readUTF().trim();
                answer = dis.readInt();
                type = dis.readInt();
                Vector options = new Vector();

                for (int j = 0; j < NUM_EXAM; ++j) {
                    
                    if (dt.cauhoi[j] == id) {
                        ++index;
                        if (opt1.length() != 0) {
                            options.addElement(opt1);
                        }
                        if (opt2.length() != 0) {
                            options.addElement(opt2);
                        }
                        if (opt3.length() != 0) {
                            options.addElement(opt3);
                        }
                        if (opt4.length() != 0) {
                            options.addElement(opt4);
                        }
                        //System.out.println("========\n " + id + content + " " + opt1 + " " + img + " " + opt2 + " " + opt3);
                        Question question = new Question(id, content, options, img, type, answer);
                        Exam ans = new Exam(index, question, 0);
                        exams.addElement(ans);
                        break;
                    }
                }

            }
            //
            if(index < NUM_EXAM) {
                Exam ans = (Exam) exams.elementAt(0);
                for(int i = 0; i < NUM_EXAM - index; ++i) {
                    Question question = new Question(ans.getId(), ans.getQuestion().getContent(), 
                            ans.getQuestion().getOptions(), ans.getQuestion().getImg(),
                            ans.getQuestion().getType(), ans.getQuestion().getAnswer());
                    Exam ansx = new Exam(index+i+1, question, 0);
                        exams.addElement(ansx);
                }
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
